﻿/*
 * LineGrid
 * 
 */
package com.cf.core.model.diagram.background {
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Point;
	
	import com.cf.core.events.LoadEvent;
	import com.cf.core.Utils.tools;
	
	import com.cf.core.model.diagram.config.*;
	
	public class LineGrid extends BaseBackground {
		// API parts
		private var G_lineColor:uint;
		public function get lineColor():uint{return G_lineColor}
		public function set lineColor(_n:uint) { G_lineColor = _n }
		private var G_rangeX:range;
		public function set rangeX(_v:range):void{ G_rangeX=_v; }
		public function get rangeX():range {return G_rangeX;}
		private var G_rangeY:range;
		public function set rangeY(_v:range):void{ G_rangeY=_v; }
		public function get rangeY():range { return G_rangeY; }
		// End : API parts
		public function LineGrid():void {
			RELEASE();
		}
		override public function RELEASE(e:*= null):void {
			this.GRAPH.graphics.clear();
			this.lineColor = 0x999999;
			G_rangeX = new range(0, 100, 10, false);
			G_rangeY = new range(0, 100, 10, false);
		}//fn
		override public function drawChart():void {
			super.drawChart();
			trace("Hi there I'm LineGrid");
		}//fn
		
		/**
		 * Create Grid by;
		 * @param	_rangeX
		 * @param	_rangeY
		 */
		override public function draw():void {
			super.draw();
			var _rangeX:range = G_rangeX;
			var _rangeY:range = G_rangeY;
			var _sizeX:Number = G_rangeX.step;
			var _sizeY:Number = G_rangeY.step;
			var _unit:Number; // 圖表中的單位
			var borderSize:int = 1;
			var rows:Number;
			var cols:Number;
			var r:int;
			var c:int;
			var i:int = 0;
			
			/****
			// draw grid line
			// grid line will depend on the MASK's width & height
			// (-2) because border will take 2pixel more.
			rows = Math.ceil(this.toHeight / (_sizeY*this.scaleY));
			cols = Math.ceil(this.toWidth / (_sizeX*this.scaleX));
			trace("Grid : x"+this.x+", y"+this.y+", w" + this.width +", h" + this.height + " row:" + rows + ", col:" + cols);
			
			if (cols > _rangeX.max) cols = _rangeX.max;
			else if(cols < _rangeX.min)	cols = _rangeX.min
			if (rows > _rangeY.max) rows = _rangeY.max;
			else if (rows < _rangeY.min) rows = _rangeY.min;
			//***/
			
			rows = _rangeY.count();
			cols = _rangeX.count();
			this.GRAPH.graphics.lineStyle(borderSize, lineColor);
			
			for(c=0; c<cols; c++){
				for(r=0; r<rows; r++){
					var toX:Number = c * _sizeX;
					var toY:Number = r * _sizeY;
					this.GRAPH.graphics.drawRect(toX, toY, _sizeX, _sizeY);
				}//for
			}//for
		}//fn
		
		override public function toString():String {
			return "com.cf.core.model.diagram.background.LineGrid";
		}//fn
		
	}//class
}//package
